package com.library.controller.user;

import com.library.dto.PageQueryDTO;
import com.library.result.PageResult;
import com.library.result.Result;
import com.library.service.BorrowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/api/user/book")
@Slf4j
@Api(tags = "图书借阅相关接口")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    /**
     * 借阅图书
     * @param bookId
     * @return
     */
    @PostMapping("/{bookId}/borrow")
    @ApiOperation("借阅图书")
    public Result<String> borrowBook(@PathVariable Long bookId) {
        log.info("借阅图书，bookId:{}", bookId);

        borrowService.borrow(bookId);

        return Result.success();
    }

    /**
     * 还回图书
     * @param bookId
     * @return
     */
    @PostMapping("/{bookId}/return")
    @ApiOperation("还回图书")
    public Result<String> returnBook(@PathVariable Long bookId) {
        log.info("还回图书，bookId:{}", bookId);

        borrowService.returnBook(bookId);

        return Result.success();
    }

    /**
     * 借阅记录
     * @param userId
     * @return
     */
    @GetMapping("/{userId}/borrow-history")
    @ApiOperation("借阅记录")
    public Result<PageResult> borrowHistory(@PathVariable Long userId,PageQueryDTO pageQueryDTO) {
        log.info("借阅记录，userId:{}", userId);

        PageResult pageResult = borrowService.borrowHistory(userId,pageQueryDTO);

        return Result.success(pageResult);
    }

}
